package com.liuyk.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * 基类adapter
 * Created by Administrator on 2015/9/11.
 */
public abstract class MyBaseAdapter<T> extends BaseAdapter {

    public Context mContext;
    private List<T> mItem;

    public MyBaseAdapter(Context context){
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mItem == null ? 0 : mItem.size();
    }

    @Override
    public Object getItem(int position) {
        return mItem == null ? 0 :mItem.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            //设为true,则他就是根,否则他是根inflater的xml文件
            convertView = LayoutInflater.from(mContext).inflate(getResource(),
                    parent,false);
        }
        refactorSetView(position, convertView, parent);
        return convertView;
    }

    public void setData(List<T> list){
        this.mItem = list;
    }

    public <T> T getData(int position){
        return (T) mItem.get(position);
    }

    public abstract int getResource();

    public abstract View refactorSetView(int position, View convertView, ViewGroup parent);

}
